/**
  ******************************************************************************
  * File Name          : ctrl_nuq.c
  * Description        : Code for data process applications
  ******************************************************************************
  * @attention
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "tskcfg.h"
#include "bsp.h"
#include "spiflash.h"
#include "collector.h"
#include "ctrl_common.h"

/* 暖气属于增温设备,lotp为关闭温度,hitp为开启温度,正常情况下hitp < lotp */
void Ctrl_nuq_mode_jt(int16_t temper)
{
  uint8_t ch;
  
  for(ch=0; ch<CTR_NUQ_CHANNEL_ALLNUM; ch++)
  {
		if(temper >= Ctrl_ParaJT.nqlotp)
    {
      nq_act[ch] = 0;
      Ctrl_StateNQ.nqst[ch] = 'l';
			continue;
    }
		
    nq_act[ch] = 2;
		if(CtrNuq_GetActuator(ch) != 0)
		{
			Ctrl_StateNQ.nqst[ch] = 'R';
		}
		else
		{
			Ctrl_StateNQ.nqst[ch] = 'r';
		}
        
    if(temper <= Ctrl_ParaJT.nqhitp)
    {
      nq_act[ch] = 1;
      Ctrl_StateNQ.nqst[ch] = 'J';
    }
    
    if(temper > (Ctrl_ParaJT.nqhitp + Ctrl_ParaJT.nqrttp))
    {
      nq_act[ch] = 0;
      Ctrl_StateNQ.nqst[ch] = 'j';
    }
  }
}

void Ctrl_nuq_mode_dl(int16_t temper, uint32_t ticker)
{
  uint8_t ch;
  
  for(ch=0; ch<CTR_NUQ_CHANNEL_ALLNUM; ch++)
  {
    if(Ctrl_ParaDL1.nqtp[ch] == 0)
    {
      if((Ctrl_ParaSK.nqen != 0) && (Ctrl_ParaSK.nqtd[ch] != 0))
      {
				Ctr_TcTickers.nuq_tc_recont = 1;
				
        if(ticker <= Ctrl_ParaSK.nqkq)
        {
          nq_act[ch] = 1;
          Ctrl_StateNQ.nqst[ch] = 'S';
        }
        else if((ticker > Ctrl_ParaSK.nqkq) && (ticker <= (Ctrl_ParaSK.nqkq + Ctrl_ParaSK.nqgb)))
        {
          nq_act[ch] = 0;
          Ctrl_StateNQ.nqst[ch] = 's';
        }
				else
        {
          nq_act[ch] = 0;
          Ctrl_StateNQ.nqst[ch] = 's';
        }
      }
      else
      {
        nq_act[ch] = 0;
        Ctrl_StateNQ.nqst[ch] = 'd';
      }
    }
    else /* Ctrl_ParaDL.fntp[ch] != 0 */
    {
      nq_act[ch] = 2;
			if(CtrNuq_GetActuator(ch) != 0)
			{
				Ctrl_StateNQ.nqst[ch] = 'E';
			}
			else
			{
				Ctrl_StateNQ.nqst[ch] = 'e';
			}
      
      if(temper <= Ctrl_ParaDL1.nqtp[ch])
      {
        nq_act[ch] = 1;
        Ctrl_StateNQ.nqst[ch] = 'D';
      }
      
      if(temper > (Ctrl_ParaDL1.nqtp[ch] + Ctrl_ParaDL2.nqrttp))
      {
        nq_act[ch] = 0;
        Ctrl_StateNQ.nqst[ch] = 'd';
      }
    }
  }
}

void Ctrl_nuq_mode_sk(int16_t temper, uint32_t ticker)
{
	uint8_t ch;
  
  for(ch=0; ch<CTR_NUQ_CHANNEL_ALLNUM; ch++)
  {
		if(Ctrl_ParaMD.nqmdsken == 0)
		{
			nq_act[ch] = 0;
			Ctrl_StateNQ.nqst[ch] = 'q';
			continue;
		}
		
    if((Ctrl_ParaSK.nqen != 0) && (Ctrl_ParaSK.nqtd[ch] != 0))
    {
			Ctr_TcTickers.nuq_tc_recont = 1;
			
      if(ticker <= Ctrl_ParaSK.nqkq)
      {
        nq_act[ch] = 1;
        Ctrl_StateNQ.nqst[ch] = 'T';
      }
      else if((ticker > Ctrl_ParaSK.nqkq) && (ticker <= (Ctrl_ParaSK.nqkq + Ctrl_ParaSK.nqgb)))
      {
        nq_act[ch] = 0;
        Ctrl_StateNQ.nqst[ch] = 't';
      }
			else
      {
        nq_act[ch] = 0;
        Ctrl_StateNQ.nqst[ch] = 't';
      }
    }
    else
    {
      nq_act[ch] = 0;
      Ctrl_StateNQ.nqst[ch] = 'p';
    }
  }
}

void Ctrl_nuq_mode_zn(int16_t temper, uint32_t ticker)
{
  uint8_t ch;
  
  for(ch=0; ch<CTR_NUQ_CHANNEL_ALLNUM; ch++)
  {
    if(temper >= Ctrl_ParaJT.nqlotp)
    {
      nq_act[ch] = 0;
      Ctrl_StateNQ.nqst[ch] = 'w';
      continue;
    }
    
    nq_act[ch] = 2;
		if(CtrNuq_GetActuator(ch) != 0)
		{
			Ctrl_StateNQ.nqst[ch] = 'X';
		}
		else
		{
			Ctrl_StateNQ.nqst[ch] = 'x';
		}
		
    if(temper <= Ctrl_ParaJT.nqhitp)
    {
      nq_act[ch] = 1;
      Ctrl_StateNQ.nqst[ch] = 'Z';
    }
    
    if(temper > (Ctrl_ParaJT.nqhitp + Ctrl_ParaJT.nqrttp))
    {
      nq_act[ch] = 0;
      Ctrl_StateNQ.nqst[ch] = 'z';
    }
    
		if((Ctrl_ParaSK.nqen != 0) && (Ctrl_ParaSK.nqtd[ch] != 0) && (temper > Ctrl_ParaJT.nqhitp) && (temper < Ctrl_ParaJT.nqlotp))
    {
			Ctr_TcTickers.nuq_tc_recont = 1;
			
      if(ticker <= Ctrl_ParaSK.nqkq)
      {
        nq_act[ch] = 1;
        Ctrl_StateNQ.nqst[ch] = 'Y';
      }
      else if((ticker > Ctrl_ParaSK.nqkq) && (ticker <= (Ctrl_ParaSK.nqkq + Ctrl_ParaSK.nqgb)))
      {
        nq_act[ch] = 0;
        Ctrl_StateNQ.nqst[ch] = 'y';
      }
			else
      {
        nq_act[ch] = 0;
        Ctrl_StateNQ.nqst[ch] = 'y';
      }
    }
  }
}

void CtrNuq_TimeCtrlISR(void)
{
	if(Ctr_TcTickers.nuq_tc_ticker_en != 0)
	{
		if(Ctr_TcTickers.nuq_tc_ticker++ > (Ctrl_ParaSK.nqkq + Ctrl_ParaSK.nqgb))
		{
			Ctr_TcTickers.nuq_tc_ticker = 0;
		}
	}
	else
	{
		Ctr_TcTickers.nuq_tc_ticker = 0;
	}
}
